package com.liu.backstage.controller;


import com.liu.backstage.model.Role;
import com.liu.backstage.model.Users;
import com.liu.backstage.service.UsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 腾杰说
 * @since 2020-07-08
 */
@Controller
@RequestMapping("/users")
public class UsersController {

    @Autowired
    private UsersService usersService;

    @RequestMapping("/findAll")
    public String findAll(Model model) {
        List<Users> list = usersService.list();
        model.addAttribute("userList",list);
        return "user-list";
    }

    @RequestMapping("/findById")
    public String findById(Integer id,Model model){
        Users users = usersService.UserById(id);
        model.addAttribute("user",users);
        return "user-show";
    }

    @RequestMapping("/findUserByIdAndAllRole")
    public String findUserByIdAndAllRole(Model model,Integer id){
        List<Role> userrole=usersService.userrole(id);
        model.addAttribute("roleList",userrole);
        model.addAttribute("userId",id);
        return "user-role-add";
    }

    @RequestMapping("/saveRole")
    @ResponseBody
    public String saveRole(Integer user,String msc){
        String[] split = msc.split(",");
        List<String> strings = Arrays.asList(split);
        usersService.InserRole(user,strings);
        return "true";
    }
}

